<?php

class Test_ModelController extends WSB_Controller_Action2
{}


class Test_Mapper extends WSB_DbMapper
{

    protected function _save(WSB_Model2 $model)
    {
        $data = $model->toArray(true);

        $members = null;
        if(array_key_exists('members', $data)) {
            $members = (array)$data['members'];
            unset($data['members']);
        }
        
        // Passwort mit MD5
        if(empty($data['password'])) {
            unset($data['password']);
        } else {
            $data['password'] = md5($data['password']);
        }
        
        $id = intval($model->id);
        if($id > 0) {
            $this->_update('feuser', $data, $id);
        } else {
            $id = $this->_insert('feuser', $data);
        }

        // Tabelle femember
        if(is_array($members)) {
            if($id > 0) {
                $this->_delete('femember', 'feuser_id=' . $id);
                foreach($members AS $groupId) {
                    $this->_insert('femember', array(
                        'fegroup_id' => $groupId,
                        'feuser_id' => $id
                    ));
                }
            }
        }

        return $id;
    }
    
    protected function _selectAll()
    {
        $select = $this->getSelect();
        $select->from('feuser');
        $select->where('deleted=0');
        return $select;
    }

    protected function _selectFind($id)
    {
        $select = $this->getSelect();
        $select->from('feuser');
        $select->where('id=?', $id);
        $select->where('deleted=0');
        return $select;
    }

}

class Test_Collection extends WSB_Collection2
{}

class Test_Model extends WSB_Model2
{}
